#include "bsp_power.h"

#include "driver/gpio.h"
#include "main.h"


void bsp_power_on(void)
{
	gpio_set_level(IO_POWER_PIN, 1);

}
void bsp_power_off(void)
{
	gpio_set_level(IO_POWER_PIN, 0);
}

SemaphoreHandle_t  bsp_power_Semaphore;

void bsp_power_send_off(void)
{
	xSemaphoreGive(bsp_power_Semaphore);//释放信号量
}


static portMUX_TYPE lock = portMUX_INITIALIZER_UNLOCKED;
static BSP_POWER_ON_TYPE power_flag=0;
void bsp_power_handler(void* params)
{
	uint32_t count=0;
	uint32_t count1=0;
	BaseType_t status = 0;
	uint8_t flag=0;
	esp_task_wdt_add(NULL);

	while (1)
	{
		esp_task_wdt_reset();

		if(bsp_key_read_power_gpio() || flag == 1)
		{
			count++;
			if(count>10*6)
			{
				if(flag == 0)
				{
					flag = 1;
					lvgl_hint_create(lv_scr_act(),lvgl_globa_text[26][system_get_Language()],200,20);
				}
			}
			if(count>10*8)
			{
				esp_restart();
			}
		}else
		{
			count = 0;
		}


		status = xSemaphoreTake(bsp_power_Semaphore, pdMS_TO_TICKS(100));//等待信号量
		if(status == pdPASS)
		{
			bap_power_debug("开始关机\r\n");
			if(power_flag == BSP_POWER_ON_TYPE_USB)
			{

				lvgl_hint_create(lv_scr_act(),lvgl_globa_text[24][system_get_Language()],200,20);
			}else if(power_flag == BSP_POWER_ON_TYPE_KEY)
			{
				//lvgl_hint_create(lv_scr_act(),"开始关机",200,20);
				//vTaskDelay(pdMS_TO_TICKS(1000));
				// while(bsp_key_read_power_gpio())
				// {
				// 	vTaskDelay(pdMS_TO_TICKS(10));
				// }
				bsp_ledc_set_duty(0);
				system_save_config();
				vTaskDelay(pdMS_TO_TICKS(500));
				bsp_power_off();

				//bsp_power_sleep();
				//wifi_lianjie(0,system_data.wifi_name,system_data.wifi_password,NULL);
				//vTaskDelay(pdMS_TO_TICKS(1000));
				vTaskDelay(pdMS_TO_TICKS(1000));
				esp_restart();
				// while(1)
				// {
				// 	bsp_power_off();
				// 	vTaskDelay(pdMS_TO_TICKS(100));
				// }

			}
		}
		//vTaskDelay(pdMS_TO_TICKS(100));
	}

	vTaskDelete(NULL);
}
#define sleep_time (1000*1000*3600*24)*30//30天
void bsp_power_sleep(void)
{
	esp_sleep_enable_ext1_wakeup((1ULL << KEY_POWER_PIN),ESP_EXT1_WAKEUP_ANY_HIGH);
	esp_sleep_enable_timer_wakeup(sleep_time);
	esp_deep_sleep_start();
}
void bsp_power_init(void)
{
	bap_power_debug("开始初始化");

	gpio_set_direction(IO_POWER_PIN, GPIO_MODE_OUTPUT);
	gpio_set_pull_mode(IO_POWER_PIN,GPIO_PULLUP_ONLY);

	gpio_set_direction(GPIO_LCD_BL, GPIO_MODE_OUTPUT);
	gpio_set_pull_mode(GPIO_LCD_BL,GPIO_PULLDOWN_ONLY);

	bsp_power_on();

	if(bsp_key_read_usb_gpio())//bsp_key_read_power_gpio()==0)
	{
		power_flag=BSP_POWER_ON_TYPE_USB;
		bap_power_debug("USB供电开机");
	}else
	{
		power_flag=BSP_POWER_ON_TYPE_KEY;
		bap_power_debug("按键电池开机");
	}

	bsp_power_Semaphore = xSemaphoreCreateCounting(10,0);//动态创建一个计数型信号量
	xTaskCreate(bsp_power_handler, "bsp_power_handler", 1024*10, NULL, 4, NULL);

	bap_power_debug("初始化结束");
}
void bsp_power_off_task_start(void)
{
	// bsp_power_Semaphore = xSemaphoreCreateCounting(10,0);//动态创建一个计数型信号量
	// xTaskCreate(bsp_power_handler, "bsp_power_handler", 1024*4, NULL, 4, NULL);
}
BSP_POWER_ON_TYPE bsp_power_get_on_type(void)
{
	if(bsp_key_read_usb_gpio())
	{
		power_flag=BSP_POWER_ON_TYPE_USB;
		bap_power_debug("USB供电开机");
	}else
	{
		power_flag=BSP_POWER_ON_TYPE_KEY;
		bap_power_debug("按键电池开机");
	}
	return power_flag;
}
